﻿using Microsoft.AspNetCore.Http;
//using Microsoft.AspNetCore.Http.Internal;
using System.IO;
using System.Text;

namespace RayD.Core
{
    /// <summary>
    /// HttpRequest 的拓展类
    /// </summary>
    public static partial class Extention
    {
        /// <summary>
        /// 判断是否为AJAX异步请求
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public static bool IsAjaxRequest(this HttpRequest request)
        {
            bool result = false;

            var xreq = request.Headers.ContainsKey("x-requested-with");
            if (xreq)
            {
                result = request.Headers["x-requested-with"] == "XMLHttpRequest";
            }

            return result;
        }
        /// <summary>
        /// 获取原始请求地址
        /// </summary>
        /// <param name="request">请求</param>
        /// <returns></returns>
        public static string GetRawUrl(this HttpRequest request)
        {
            return new StringBuilder()
                .Append(request.PathBase)
                .Append(request.Path)
                .Append(request.QueryString)
                .ToString();
        }
        /// <summary>
        /// 获取去掉查询参数的Url
        /// </summary>
        /// <param name="request">请求</param>
        /// <returns></returns>
        public static string GetDisplayUrlNoQuery(this HttpRequest request)
        {
            string queryStr = request.QueryString.ToString();
            string displayUrl = request.Path;

            return queryStr.IsEmpty() ? displayUrl : displayUrl.Replace(queryStr, "");
        }
        /// <summary>
        /// 获取Url的查询参数
        /// </summary>
        /// <param name="request">请求</param>
        /// <returns></returns>
        public static string GetUrlParams(this HttpRequest request)
        {
            return request.QueryString.ToString();
        }
        /// <summary>
        /// 获取Post请求参数
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public static string GetPostParams(this HttpRequest request)
        {
            string requestParams = string.Empty;
            if (request.ContentLength != 0)
            {
                if (request.ContentType == "application/x-www-form-urlencoded")
                {
                    requestParams += "{";
                    //基于application/x-www-form-urlencoded的Content-Type
                    foreach (var key in request.Form.Keys)
                    {
                        requestParams += $"\"{key}\" :{ request.Form[key].ToString()} ,";
                    }
                    requestParams = requestParams.TrimEnd(',');
                    requestParams += "}";
                }
                else
                {
                    //request.EnableRewind();
                    request.Body.Position = 0;
                    Stream stream = request.Body;
                    byte[] buffer = new byte[request.ContentLength.Value];
                    stream.Read(buffer, 0, buffer.Length);
                    requestParams = Encoding.UTF8.GetString(buffer);
                    stream.Close();
                }
            }
            return requestParams;
        }
    }
}
